﻿// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "FLL/chapter8/透明度测试"
{
    Properties
    {
        _Color("[漫反射颜色] color",Color)=(1,1,1,1)
        _Cutoff("[透明度剔除条件] Alpha Cutoff",Range(0,1))=0.5
        _MainTex ("[主纹理] Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutoff"}

        //-------------------------------第一个pass只渲染背面
         Pass
        {
            Tags{"LightMode"="ForwardBase"}
            //打开剔除,剔除前面
            Cull Front
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;
            fixed _Cutoff;

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float4 texcoord:TEXCOORD0;
            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
                float2 uv:TEXCOORD2;

            };



            v2f vert (appdata v)
            {
                v2f o;
                o.pos=UnityObjectToClipPos(v.vertex);
                o.worldNormal=UnityObjectToWorldNormal(v.normal);
                o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 worldNormal=normalize(i.worldNormal);
                fixed3 worldLightDir=normalize(UnityWorldSpaceLightDir(i.worldPos));

                fixed4 texColor=tex2D(_MainTex,i.uv);

                //----------------------[透明度测试 (alpha test)]-----------------------
                //用texcolor减去_Cutoff, 
                //则texColor中小于_Cutoff的值都变成了负数,负数部分的像素被剔除
                clip(texColor.a-_Cutoff);
                
                fixed3 albedo=texColor.rgb*_Color.rgb;
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(worldNormal,worldLightDir));

                return fixed4(ambient+diffuse,1.0);


            }
            ENDCG
        }
        

            //----------------[第二个pass,渲染前面]--------------------
            Pass
            {
                Tags{"LightMode"="ForwardBase"}
                Cull Back
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"
                #include "Lighting.cginc"

                sampler2D _MainTex;
                float4 _MainTex_ST;
                float4 _Color;
                fixed _Cutoff;

                struct appdata
                {
                    float4 vertex : POSITION;
                    float3 normal:NORMAL;
                    float4 texcoord:TEXCOORD0;
                };

                struct v2f
                {
                    float4 pos:SV_POSITION;
                    float3 worldNormal:TEXCOORD0;
                    float3 worldPos:TEXCOORD1;
                    float2 uv:TEXCOORD2;

                };



                v2f vert (appdata v)
                {
                    v2f o;
                    o.pos=UnityObjectToClipPos(v.vertex);
                    o.worldNormal=UnityObjectToWorldNormal(v.normal);
                    o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                    o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
                    return o;
                }

                fixed4 frag (v2f i) : SV_Target
                {
                    fixed3 worldNormal=normalize(i.worldNormal);
                    fixed3 worldLightDir=normalize(UnityWorldSpaceLightDir(i.worldPos));

                    fixed4 texColor=tex2D(_MainTex,i.uv);

                    //----------------------[透明度测试 (alpha test)]-----------------------
                    //用texcolor减去_Cutoff, 
                    //则texColor中小于_Cutoff的值都变成了负数,负数部分的像素被剔除
                    clip(texColor.a-_Cutoff);
                    
                    fixed3 albedo=texColor.rgb*_Color.rgb;
                    fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                    fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(worldNormal,worldLightDir));

                    return fixed4(ambient+diffuse,1.0);


                }
                ENDCG
        }
    }
    FallBack"Transparent/Cutout/VertexLit"
}
